package com.huaxin.hxmodulestatisticalanalysis.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huaxin.hxmodulestatisticalanalysis.models.DeviceAlarm;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;

@Mapper
public interface AlarmMapper extends BaseMapper<DeviceAlarm> {
    /**
     * 统计指定组织下当前未恢复的告警数量
     */
    @Select("SELECT COUNT(*) " +
            "FROM device_alarms " +
            "WHERE org_id = #{orgId} " +
            "  AND is_resolved = 0 " +
            "  AND is_deleted = 0")
    Integer countUnresolvedAlarms(@Param("orgId") String orgId);

    /**
     * 获取最近一条未恢复告警的发生时间（可选增强）
     */
    @Select("SELECT MAX(created_at) " +
            "FROM device_alarms " +
            "WHERE org_id = #{orgId} " +
            "  AND is_resolved = 0 " +
            "  AND is_deleted = 0")
    LocalDateTime getLastUnresolvedAlarmTime(@Param("orgId") String orgId);
}
